package a10_动态规划;

/**
 * <p>
 * a31_打家劫舍3复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/23
 */
public class a31_打家劫舍3复习3 {

    public int rob(TreeNode root) {
        int[] traversal = traversal(root);
        return Math.max(traversal[0],traversal[1]);
    }

    //0偷
    //1不偷
    private int[] traversal(TreeNode node){
        if (node == null) {
            return new int[2];
        }

        int[] traversal1 = traversal(node.left);
        int[] traversal2 = traversal(node.right);

        int[] result = new int[2];
        //偷
        result[0] = traversal1[1] + traversal2[1] + node.val;
        //不偷
        result[1] = Math.max(traversal1[0], traversal1[1]) + Math.max(traversal2[0], traversal2[1]);
        return result;
    }
}
